package com.example.hockdemo

import android.util.Log
import de.robv.android.xposed.IXposedHookLoadPackage
import de.robv.android.xposed.XC_MethodHook
import de.robv.android.xposed.XposedBridge
import de.robv.android.xposed.XposedHelpers
import de.robv.android.xposed.callbacks.XC_LoadPackage.LoadPackageParam

/**
 * create by zwp on 2024/3/17 16:36
 * description:
 */
class HockTest : IXposedHookLoadPackage {
    /**
     * xpose插件入口点
     * Module继承了IXposedHookLoadPackage接口，当系统加载应用包的时候回回调 handleLoadPackage；
     *
     * @param loadPackageParam
     * @throws Throwable
     */
    @Throws(Throwable::class)
    override fun handleLoadPackage(loadPackageParam: LoadPackageParam) {
        // 获取加载的apk程序包名
        XposedBridge.log("当前启动的应用程序是: " + loadPackageParam.packageName)
        XposedBridge.log("Hook成功咯，宝们～_~!")

        //过滤包名，定位要Hook的包名
        if (loadPackageParam.packageName.equals("com.example.myapplication")) {

            //定位要Hook的具体的类名
            val clazz =
                loadPackageParam.classLoader.loadClass("com.example.myapplication.MainActivity")
            //Hook的方法为toastMessage，XposedHelpers的静态方法
            //findAndHookMethod就是hook函数的的方法，
            //其参数对应为   类名+loadPackageParam.classLoader（照写）+方法名+参数类型（根据所hook方法的参数的类型，即有多少个写多少个，加上.class）+XC_MethodHook回调接口；
            //这里就表示我hock的 MainActivity里面的toastMessage方法  如果有参数 例如是一个string 就多来一个String::class.java,
            //例如XposedHelpers.findAndHookMethod(clazz, "toastMessage", String::class.java,object : XC_MethodHook() {
            XposedHelpers.findAndHookMethod(clazz, "toastMessage", object : XC_MethodHook() {
                override fun beforeHookedMethod(param: MethodHookParam?) {
                    super.beforeHookedMethod(param)
                }

                override fun afterHookedMethod(param: MethodHookParam?) {
                    param?.result = "哦吼，同学你已被劫持-111"
                }
            })
        }
    }
}